package com.xiaodong.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaodong.reggie.currency.CostomException;
import com.xiaodong.reggie.currency.R;
import com.xiaodong.reggie.entity.Category;
import com.xiaodong.reggie.entity.Dish;
import com.xiaodong.reggie.entity.Employee;
import com.xiaodong.reggie.entity.Setmeal;
import com.xiaodong.reggie.mapper.CategoryMapper;
import com.xiaodong.reggie.mapper.EmployeeMapper;
import com.xiaodong.reggie.service.CategoryService;
import com.xiaodong.reggie.service.DishService;
import com.xiaodong.reggie.service.EmployeeService;
import com.xiaodong.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {

        //关联菜品
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper();

        queryWrapper1.eq(Dish::getCategoryId, id);

        int dish = dishService.count(queryWrapper1);

        if (dish > 0) {

            throw new CostomException("菜品已被关联");

        }

        //关联套餐
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper();

        queryWrapper2.eq(Setmeal::getCategoryId, id);

        int setmeal = setmealService.count(queryWrapper2);

        if (setmeal > 0) {
            throw new CostomException("套餐已被关联");
        }

        //可以删除
        super.removeById(id);

    }
}
